package com.letoken.platform.repo.app.dao;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.repo.admin.req.user.AdminBalanceListReq;
import com.letoken.platform.repo.admin.res.user.AdminBalanceListRes;
import com.letoken.platform.repo.app.po.AppUserWallet;
import com.letoken.platform.repo.app.req.user.BalanceHistoryReq;
import com.letoken.platform.repo.app.res.user.AppBalanceHistoryRes;
import com.letoken.platform.repo.mall.po.AppBalanceWallet;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 余币报资产 Mapper 接口
 * </p>
 *
 * @author author
 * @since 2023-07-25
 */
public interface AppBalanceWalletMapper extends BaseMapper<AppBalanceWallet> {

    @Select("select * from app_balance_wallet where id = #{id} for update")
    AppBalanceWallet selectForUpdate(@Param("id") Integer id);


    @Select("select IFNULL(SUM(amount),0) from app_balance_history where user_id  = #{userId} and create_time > #{excludeTime} and type = 0")
    BigDecimal excludeDissatisfaction(@Param("userId") Integer userId, @Param("excludeTime")LocalDateTime excludeTime);


    @Select("select id from app_balance_wallet where user_id = #{userId} and token_id = #{tokenId} ")
    Integer getIdByUserId(@Param("tokenId") Integer tokenId,@Param("userId") Integer userId);

    @Select("select * from app_balance_wallet where user_id = #{userId} and token_id = #{tokenId} ")
    AppBalanceWallet getRecord(@Param("userId") Integer userId , @Param("tokenId") Integer tokenId);


    @Select("select sum(balance) as balance from app_balance_wallet ")
    AppBalanceWallet totalData();

    @Select("select  IFNULL(sum(amount),0) as income from app_balance_history where  create_time >= '2023-10-12 12:00:00' and type = 2")
    AppBalanceWallet totalIncome();

    Page<AdminBalanceListRes> balanceRecord(@Param("page") Page<AdminBalanceListRes> page , @Param("req") AdminBalanceListReq req);


    @Select("select id,user_id from app_balance_wallet where balance >= 1 ")
    List<AppBalanceWallet> allBalanceUser();


}
